package com.weixing.mall.provider.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.core.annotation.SysLog;
import com.weixing.mall.core.enums.OperateEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.model.domain.AgentRule;
import com.weixing.mall.provider.model.dto.AgentRuleDto;
import com.weixing.mall.provider.model.enums.AgentRuleStatusEnum;
import com.weixing.mall.provider.model.query.AgentRuleQuery;
import com.weixing.mall.provider.model.vo.AgentRuleVo;
import com.weixing.mall.provider.service.IAgentRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 代理商晋升/折扣规则Api
 *
 * @author  gjq
 * @date  2019/9/23
 * @since  1.0
 **/
@Validated
@RestController
@RequestMapping("agent/rule")
public class AgentRuleController extends BaseController {

    @Autowired
    private IAgentRuleService agentRuleService;

    /**
     * 加载代理商晋升/折扣规则分页列表
     * @param agentRuleQuery 查询条件
     * @return 结果
     */
    @GetMapping("")
    public Result<IPage<AgentRuleVo>> list(AgentRuleQuery agentRuleQuery) {
        IPage<AgentRuleVo> pageInfo = agentRuleService.selectOfPage(initMpPage(), agentRuleQuery);
        return ResultUtil.success(pageInfo);
    }

    /**
     * 加载代理商折扣列表
     * @return 结果
     */
    @GetMapping("/list")
    public Result listALL() {
      List<AgentRule> ruleList=agentRuleService.list(new QueryWrapper<AgentRule>().isNotNull("id").orderByAsc("id"));
        return ResultUtil.success(ruleList);
    }


    /**
     * 保存代理商晋升/折扣规则信息
     * @param agentRuleDto 参数
     * @return 结果
     */
    @PostMapping("save")
    @NoRepeatSubmit
    @SysLog(type = OperateEnum.S, desp = "编辑代理商晋升折扣规则")
    public Result save(AgentRuleDto agentRuleDto) {
        String ruleName = agentRuleDto.getRuleName();
        int agentLevel = agentRuleDto.getAgentLevel().getValue();
        int agentType = agentRuleDto.getAgentType().getValue();
        if (null == agentRuleDto.getId()) {
            AgentRule agentRule = agentRuleService.selectByRuleName(ruleName);
            if (null != agentRule) {
                return ResultUtil.error("已存在同名规则");
            }
            agentRule = agentRuleService.selectByLevelAndType(agentLevel, agentType);
            if (null != agentRule) {
                return ResultUtil.error("已存在同等级同类型的规则");
            }
        }

        agentRuleService.save(agentRuleDto);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除代理商晋升/折扣规则信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "代理商晋升规则id", paramType = "path", dataType = "long")
    })
    @PostMapping("del/{id}")
    @SysLog(type = OperateEnum.S, desp = "删除代理商晋升折扣规则")
    public Result del(@PathVariable long id) {
        AgentRule agentRule = agentRuleService.getById(id);
        if (AgentRuleStatusEnum.STATUS1.getValue().intValue() == agentRule.getRuleStatus()) {
            return ResultUtil.error("规则已发布禁止删除");
        }
        return agentRuleService.removeById(id) ? ResultUtil.success() : ResultUtil.error();
    }

    @ApiOperation(value = "发布")
    @PostMapping("pub/{id}")
    public Result chgRuleStatus(@PathVariable Long id) {
        AgentRule agentRule = agentRuleService.getById(id);
        agentRule.setRuleStatus(AgentRuleStatusEnum.STATUS1.getValue());
        agentRuleService.updateById(agentRule);
        return ResultUtil.success();
    }

    @ApiOperation(value = "根据代理等级和代理类型获取代理折扣")
    @PostMapping("getDisCount/{level}/{type}")
    public Result getDisCountByLevelAndType(@PathVariable Integer level, @PathVariable Integer type) {
        AgentRule agentRule = agentRuleService.selectByLevelAndType(level, type);
        if (null == agentRule) {
            return ResultUtil.error("系统未配置该代理等级的折扣信息，请联系系统管理员确认！");
        } else {
            return ResultUtil.success(agentRule.getDiscount());
        }
    }

}
